<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *订单管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$merchid=plugin('jy_merch')->checklogin();
$manage_op=array('confirm','cancel','agree','disagree');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择订单！','error');
	}
}
$tabs=array(
	array('op'=>'list','title'=>'订单列表'),
);
$return['status']=$this->order_status;
if($_RQ['op']=='list'){
	$params=array('merchid'=>$merchid);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','total','serve_id','package','user_id','worker_id','serve_time','day','timeid','money','status','tuikuan_time','mobile','username','pay','remark','coupon','createtime');
	$list=pdo_getslice('jy_yuyue_order',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$user=pdo_get('jy_yuyue_user',array('id'=>$item['user_id'],'uniacid'=>$_SESSION['uniacid']));
			$item['worker_name']=pdo_getcolumn('jy_yuyue_worker',array('id'=>$item['worker_id'],'uniacid'=>$_SESSION['uniacid']),'name');
			$item['worker_name']=$item['worker_name']?$item['worker_name']:'暂无';
			$item['serve_title']=pdo_getcolumn('jy_yuyue_serve',array('id'=>$item['serve_id'],'uniacid'=>$_SESSION['uniacid']),'title');
			//$item['username']=$user['name'];
			//$item['user_mobile']=$user['mobile'];
			$item['paytype']=pdo_getcolumn('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'oid'=>$item['id'],'plugin'=>'jy_yuyue'),'pay_type');
			$item['paytype']=get_pay_types()[$item['paytype']];
			$package=json_decode($item['package'],true);
			$item['package']=$package['package_title'];
			$serve_times=pdo_getcolumn('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['serve_id']),'serve_time');
			$serve_times=json_decode($serve_times,true);
			$item['serve_time']=$item['day'].'--'.$serve_times[$item['timeid']]['times']['start'].'-'.$serve_times[$item['timeid']]['times']['end'];
			$item['total']=$item['total']?:1;
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'username','title'=>'用户姓名','type'=>'string'),
			array('name'=>'worker_name','title'=>'员工姓名','type'=>'string'),
			array('name'=>'mobile','title'=>'用户电话','type'=>'string'),
			array('name'=>'serve_title','title'=>'项目名称','type'=>'string'),
			array('name'=>'package','title'=>'套餐','type'=>'string'),
			array('name'=>'total','title'=>'数量','type'=>'string'),
			array('name'=>'serve_time','title'=>'服务时间','type'=>'string'),
			array('name'=>'money','title'=>'金额','type'=>'string'),
			array('name'=>'pay','title'=>'实际支付','type'=>'string'),
			array('name'=>'paytype','title'=>'支付方式','type'=>'string'),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
			array('name'=>'coupon','title'=>'优惠券','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		),'action'=>array(
			array('type'=>'href','op'=>'look','title'=>'自定义表单数据'),
			array('type'=>'post','op'=>'confirm','title'=>'确认','message'=>'确认接收订单？','show'=>array('status'=>1)),
			array('type'=>'post','op'=>'cancel','title'=>'取消','message'=>'取消订单？','show'=>array('status'=>1)),
			array('type'=>'modal','op'=>'setworker','title'=>'指派技师','show'=>array('status'=>array(1,2))),
			array('type'=>'modal','op'=>'payinfo','title'=>'支付信息','show'=>array('pay_id <>'=>0)),
			array('type'=>'href','op'=>'uptime','title'=>'修改预约时间','show'=>array('status'=>array(1,2))),
		)),
		'search'=>array(
			array('name'=>'username','width'=>15,'title'=>'用户姓名','type'=>'string'),
			array('name'=>'worker_name','width'=>15,'title'=>'员工姓名','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'radio','options'=>'status'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
//确认订单操作
if($_RQ['op']=='confirm'){
	if($_RQ['id']){
	
		pdo_update('jy_yuyue_order',array('status'=>'2'),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
		
	}
	exi('确认订单成功！');
}
//取消订单操作
if($_RQ['op']=='cancel'){
	if($_RQ['id']){
		$this->model('model')->order_cancel($_RQ['id']);
	}
	exi('取消订单成功！');
}
//同意退款
if($_RQ['op']=='agree'){
	if($_RQ['id']){
	
		pdo_update('jy_yuyue_order',array('status'=>'6'),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
		
	}
	exi('已同意退款申请！');
}
//拒绝退款操作
if($_RQ['op']=='disagree'){
	if($_RQ['id']){
	
		pdo_update('jy_yuyue_order',array('status'=>'7'),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
		
	}
	exi('已拒绝退款申请！');
}

if($_RQ['op']=='look'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_yuyue_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		exi('请选择订单','error');
	}
	$user=pdo_get('jy_yuyue_user',array('id'=>$item['user_id'],'uniacid'=>$_SESSION['uniacid']));
			$item['worker_name']=pdo_getcolumn('jy_yuyue_worker',array('id'=>$item['worker_id'],'uniacid'=>$_SESSION['uniacid']),'name');
			$item['worker_name']=$item['worker_name']?$item['worker_name']:'暂无';
			$item['serve_title']=pdo_getcolumn('jy_yuyue_serve',array('id'=>$item['serve_id'],'uniacid'=>$_SESSION['uniacid']),'title');
			//$item['username']=$user['name'];
			//$item['user_mobile']=$user['mobile'];
			$item['paytype']=pdo_getcolumn('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'oid'=>$item['id'],'plugin'=>'jy_yuyue'),'pay_type');
			$item['paytype']=get_pay_types()[$item['paytype']];
			$package=json_decode($item['package'],true);
			$item['package']=$package['package_title'];
			$serve_times=pdo_getcolumn('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['serve_id']),'serve_time');
			$serve_times=json_decode($serve_times,true);
			$item['serve_time']=$item['day'].'--'.$serve_times[$item['timeid']]['times']['start'].'-'.$serve_times[$item['timeid']]['times']['end'];
			$item['total']=$item['total']?:1;
	$item['fileds']=$item['fileds']?json_decode($item['fileds'],true):array();
	$formtypes=get_form_types();	
	foreach($item['fileds'] as &$v){
		if($v['type']==5 && $v['data']){
			$v['data'] = explode(";", $v['data']);
			foreach($v['data'] as $k=>&$i){
				if(empty($v['data'][$k])){
					unset($v['data'][$k]);
				}
			}
		}
		//$v['type']=$formtypes[$v['type']];
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'look',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回订单列表'),
		),
		'desc'=>'订单信息',
		'look'=>array(
				array('name'=>'username','title'=>'用户姓名','type'=>'string'),
				array('name'=>'worker_name','title'=>'员工姓名','type'=>'string'),
				array('name'=>'mobile','title'=>'用户电话','type'=>'string'),
				array('name'=>'serve_title','title'=>'项目名称','type'=>'string'),
				array('name'=>'package','title'=>'套餐','type'=>'string'),
				array('name'=>'serve_time','title'=>'服务时间','type'=>'string'),
				array('name'=>'money','title'=>'金额','type'=>'string'),
				array('name'=>'pay','title'=>'实际支付','type'=>'string'),
				array('name'=>'remark','title'=>'备注','type'=>'string'),
				array('name'=>'coupon','title'=>'优惠券','type'=>'string'),
				array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
				array(
						'title'=>'表单详情',
						'type'=>'array',
						'name'=>'fileds',
						'list'=>array(
							array('type'=>'string','name'=>'title','title'=>'表单标题'),	
							array('type'=>'labelarray','name'=>'data','title'=>'表单内容'),	
						),
				)
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$params['uniacid']=$_SESSION['uniacid'];
	$order=pdo_getall('jy_yuyue_order',$params);
	if($order){
		foreach($order as &$item){
			$user=pdo_get('jy_yuyue_user',array('id'=>$item['user_id'],'uniacid'=>$_SESSION['uniacid']));
			$item['worker_name']=pdo_getcolumn('jy_yuyue_worker',array('id'=>$item['worker_id'],'uniacid'=>$_SESSION['uniacid']),'name');
			$item['worker_name']=$item['worker_name']?$item['worker_name']:'暂无';
			$item['serve_title']=pdo_getcolumn('jy_yuyue_serve',array('id'=>$item['serve_id'],'uniacid'=>$_SESSION['uniacid']),'title');
			$user=pdo_get('jy_yuyue_user',array('id'=>$item['user_id'],'uniacid'=>$_SESSION['uniacid']));
			//$item['username']=$user['name'];
			//$item['user_mobile']=$user['mobile'];
			$item['paytype']=pdo_getcolumn('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'oid'=>$item['id'],'plugin'=>'jy_yuyue'),'pay_type');
			$item['paytype']=get_pay_types()[$item['paytype']];
			$package=json_decode($item['package'],true);
			$item['package']=$package['package_title'];
			$serve_times=pdo_getcolumn('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['serve_id']),'serve_time');
			$serve_times=json_decode($serve_times,true);
			$item['serve_time']=$item['day'].'--'.$serve_times[$item['timeid']]['times']['start'].'-'.$serve_times[$item['timeid']]['times']['end'];
			$item['status']=$this->order_status[$item['status']];
		}
	}
	$header=array(
		'ordersn'=>'订单号',
		'username'=>'用户姓名',
		'worker_name'=>'员工姓名',
		'mobile'=>'用户电话',
		'serve_title'=>'项目名称',
		'package'=>'套餐',
		'serve_time'=>'服务时间',
		'money'=>'金额',
		'pay'=>'实际支付',
		'paytype'=>'支付方式',
		'coupon'=>'优惠券',
		'remark'=>'备注',
		'status'=>'状态',
	);
	cfc('tools')->export2excel($header,$order,'订单');
}
if($_RQ['op']=='setworker'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$update=array(
			'worker_id'=>intval($post['worker_id']),
		);
		pdo_update('jy_yuyue_order',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		model('jy_yuyue/order')->order_pai($post);
		exi('设置成功！','','//list/');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_yuyue_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'手动派工',
	);
	$htmls['edit']=array(
		array('type'=>'select','name'=>'worker_id','title'=>'手动派工给技师？','show'=>array('status'=>array(1,2)),'options'=>'workers','option_title'=>'name'),
	);
	$return['htmls']=$htmls;
	$return['workers']=pdo_getall('jy_yuyue_worker',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),array('id','name'));	
	exi($return);
}
if($_RQ['op']=='uptime'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		//判断待支付订单及判断是否超过可预订次数
		$return['basic']=$this->plugin_setting('basic');
		$data=$this->days($post['serve_id'],$return['basic']['days']);
		$serve=pdo_get('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['serve_id']));
		$serve_time=json_decode($serve['serve_time'],true);
		$post['day']=$post['day'];
		$num=pdo_getsum('jy_yuyue_order',array('timeid'=>$post['timeid'],'uniacid'=>$_SESSION['uniacid'],'day'=>$post['day'],'serve_id'=>$post['serve_id'],'status'=>array(1,2,4)),'total');
		if(($serve_time[$post['timeid']]['yuyue'])<=$num){
			exi('该时间段预约已满，请重新选择预约时间段！','error');
		}
		if($serve['xianzhi']==1){
			//某天
			$todaysum=pdo_getsum('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'user_id'=>$_SESSION['uid'],'serve_id'=>$post['serve_id'],'status'=>array(1,2,4),'day'=>$data[$post['dayid']]['ymd']),'total');
			if($todaysum>=$serve['daytimes'] && $serve['daytimes']){
				exi('你可预约该项目该日次数已达上限,请选择其他日期','error');
			}
			//所有
			$allsum=pdo_getsum('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'user_id'=>$_SESSION['uid'],'serve_id'=>$post['serve_id'],'status'=>array(1,2,4)),'total');
			if($allsum>=$serve['alltimes'] && $serve['alltimes']){
				exi('你可预约总次数已达上限,请选择其他项目','error');
			}
		
		}
		$update=array(
			'timeid'=>$post['timeid'],
			'day'=>$post['day'],
		);
		pdo_update('jy_yuyue_order',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('设置成功！','','//list/');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_yuyue_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置预约时间',
	);
	$htmls['edit']=array(
		array('type'=>'time','name'=>'day','time_type'=>'day','title'=>'日期'),
		array('type'=>'radio','name'=>'timeid','title'=>'时间段','options'=>'serve_time'),
	);
	$serve=pdo_get('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['serve_id']));
	$serve_time=json_decode($serve['serve_time'],true);
	$return['serve_time']=array();
	foreach($serve_time as $sv){
		$return['serve_time'][]=$sv['times']['start'].'~'.$sv['times']['end'];
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='payinfo'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择订单！','error');
	}
	$order=pdo_get('jy_yuyue_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$item=pdo_get('core_paylog',array('id'=>$order['pay_id'],'uniacid'=>$_SESSION['uniacid']));
	$return['htmls']=array(
		'type'=>'look',
		'desc'=>'支付详情',
		'look'=>array(
			array('name'=>'tid','title'=>'支付单号','type'=>'string'),
			array('name'=>'status','title'=>'支付状态','type'=>'radio','options'=>'statuss'),
			array('name'=>'pay_type','title'=>'支付方式','type'=>'radio','options'=>'pay_types'),
			array('name'=>'createtime','title'=>'发起支付时间','type'=>'time'),
			array('name'=>'fee','title'=>'支付金额','type'=>'string'),
			array('name'=>'di_fee','title'=>'抵扣金额','type'=>'string'),
		),
	);
	$item['diyfields']=$item['diyfields']?json_decode($item['diyfields'],true):array();
	foreach($item['diyfields'] as &$v){
			if($v['type']==5 && $v['data']){
				$v['data'] = explode(";", $v['data']);
				foreach($v['data'] as $k=>&$i){
					if(empty($v['data'][$k])){
						unset($v['data'][$k]);
					}
				}
			}
			//$v['type']=$formtypes[$v['type']];
	}
	if($item['diyfields'][0]){
		$return['htmls']['look'][]=array(
			'title'=>'支付内容',
			'type'=>'array',
			'name'=>'diyfields',
			'list'=>array(
				array('type'=>'string','name'=>'title','title'=>'表单标题'),	
				array('type'=>'labelarray','name'=>'data','title'=>'表单内容'),	
			),
		);
	}
	$return['statuss']=array(
		'0'=>'待支付',
		'1'=>'已支付',
		'2'=>'支付失败'
	);
	$return['pay_types']=get_pay_types();
	$return['item']=$item;
	exi($return);
}